package org.abacus.platform.database;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.abacus.platform.data.ConnectionProvider;

public abstract class Database {

	private static Map<String, Database> databases = Collections.synchronizedMap(new HashMap<String, Database>());

	private ConnectionProvider connectionProvider;

	private static final String DATABASE_IMPL = "org.abacus.platform.database.impl.DatabaseImpl";

	public final static Database getInstance() {
		return getInstance(DATABASE_IMPL);
	}

	public synchronized final static Database getInstance(String className) {
		Database database = databases.get(className);
		if (null != database) {
			return database;
		}
		try {
			database = (Database) Class.forName(className).newInstance();
			databases.put(className, database);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return databases.get(className);
	}

	public ConnectionProvider getConnectionProvider() {
		return connectionProvider;
	}

	public void setConnectionProvider(ConnectionProvider connectionProvider) {
		this.connectionProvider = connectionProvider;
	}

	public abstract DatabaseManager getDatabaseManager();

	public abstract Table prepareTable();

	public abstract TableGroup prepareTableGroup();

	public abstract Column prepareColumn();

	public abstract Datasource prepareDatasource();
}
